package com.ruoyi.system.service.impl;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.file.FileTypeUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.File;
import com.ruoyi.system.mapper.SysFileMapper;
import com.ruoyi.system.service.ISysFileService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * 文件 服务层实现
 */
@Service
public class SysFileServiceImpl implements ISysFileService {
    @Autowired
    private SysFileMapper fileMapper;

    /**
     * 查询文件列表
     */
    @Override
    public List<File> selectFileList(File file) {
        return fileMapper.selectFileList(file);
    }

    /**
     * 查询文件信息
     */
    @Override
    public File selectFileById(Long fileId) {
        return fileMapper.selectFileById(fileId);
    }

    /**
     * 上传文件
     */
    @Override
    public File uploadFile(MultipartFile file) throws Exception {
        String originalFilename = file.getOriginalFilename();
        String extension = FileTypeUtils.getExtension(originalFilename);

        // 创建文件名
        String fileName = DateUtils.datePath() + "/" + IdUtils.fastUUID() + extension;

        java.io.File dest = new java.io.File(RuoYiConfig.getUploadPath(), fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }

        // 保存文件
        file.transferTo(dest);

        // 保存文件信息
        File sysFile = new File();
        sysFile.setName(originalFilename);
        sysFile.setUrl(fileName);
        sysFile.setSize(FileUtils.getSize(file.getSize()));
        sysFile.setType(extension);
        sysFile.setFilePath(dest.getAbsolutePath());
        sysFile.setOriginalName(originalFilename);
        sysFile.setDelFlag("0");
        fileMapper.insertFile(sysFile);

        return sysFile;
    }

    /**
     * 下载文件
     */
    @Override
    public void downloadFile(Long fileId, OutputStream output) throws Exception {
        File sysFile = fileMapper.selectFileById(fileId);
        if (sysFile == null) {
            throw new ServiceException("文件不存在");
        }

        String filePath = RuoYiConfig.getUploadPath() + sysFile.getUrl();
        java.io.File file = new java.io.File(filePath);
        if (!file.exists()) {
            throw new ServiceException("文件不存在");
        }

        try (FileInputStream input = new FileInputStream(file)) {
            IOUtils.copy(input, output);
            output.flush();
        }
    }

    /**
     * 删除文件信息
     */
    @Override
    public int deleteFile(Long fileId) {
        File sysFile = fileMapper.selectFileById(fileId);
        if (sysFile != null) {
            String filePath = RuoYiConfig.getUploadPath() + sysFile.getUrl();
            FileUtils.deleteFile(filePath);
        }
        return fileMapper.deleteFileById(fileId);
    }

    /**
     * 批量删除文件信息
     */
    @Override
    public int deleteFileByIds(Long[] fileIds) {
        for (Long fileId : fileIds) {
            File sysFile = fileMapper.selectFileById(fileId);
            if (sysFile != null) {
                String filePath = RuoYiConfig.getUploadPath() + sysFile.getUrl();
                FileUtils.deleteFile(filePath);
            }
        }
        return fileMapper.deleteFileByIds(fileIds);
    }

    @Override
    public int insertFile(File file) {
        return fileMapper.insertFile(file);
    }
}